package com.yxc.elastic.plugin.lcs;

import org.elasticsearch.script.*;

import java.util.Map;
import java.util.Set;

/**
 * @author: YXC
 * @time: 2024/1/3 16:40
 **/
public class LcsScriptEngine implements ScriptEngine {

    /**
     * 脚本名字
     */
    private static final String SCRIPT_NAME = "lsc_similarity";
    private static final String SCRIPT_SOURCE = "lsc";

    @Override
    public String getType() {
        return SCRIPT_NAME;
    }

    @Override
    public <FactoryType> FactoryType compile(String name, String code, ScriptContext<FactoryType> context, Map<String, String> params) {

        if (!context.equals(ScoreScript.CONTEXT)) {
            throw new IllegalArgumentException(getType()
                    + " scripts cannot be used for context ["
                    + context.name + "]");
        }
        if (!code.equals(SCRIPT_SOURCE)){
            throw new IllegalArgumentException("Unknown script name " + code);
        }

        LcsSimilarityFactory factory = new LcsSimilarityFactory();

        return context.factoryClazz.cast(factory);
    }

    @Override
    public Set<ScriptContext<?>> getSupportedContexts() {
        return null;
    }

}

